package com.itheima.service.impl;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.PageResult;
import com.itheima.mapper.SetmealMapper;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.JedisPool;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = SetmealService.class)
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private JedisPool jedisPool;

    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public void add(Integer[] checkGroupIds, Setmeal setmeal) {

        //新增套餐
        setmealMapper.add(setmeal);

        //设置套餐和检查组的中间表关系
        setSetmealAndCheckGroup(checkGroupIds,setmeal.getId());

        if (setmeal.getImg()!=null){
            //将保存到数据库的图片保存一份到redis的set集合中
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,setmeal.getImg());
        }

    }

    private void setSetmealAndCheckGroup(Integer[] checkGroupIds, Integer id) {
        if (checkGroupIds!=null && checkGroupIds.length>0){

            setmealMapper.setSetmealAndCheckGroup(checkGroupIds,id);

        }

    }

    /**
     * 分页查询
     * @param currentPage
     * @param pageSize
     * @param queryString
     * @return
     */
    @Override
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {

        //定义页面大小和当前页的页码
        PageHelper.startPage(currentPage,pageSize);

        //条件查询数据
        List<Setmeal> setmealList = setmealMapper.findByCondition(queryString);

        PageInfo<Setmeal> pageInfo = new PageInfo<>(setmealList);

        return new PageResult(pageInfo.getTotal(),pageInfo.getList());
    }

    //查询所有套餐
    @Override
    public String findSetmealAll() {
        //1.首先从redis中查询
        String json = jedisPool.getResource().get("setmealAllRemote");
        if (StringUtils.isEmpty(json)) {
            //2.如果redis中没有数据，那么就去数据库查询，并放入redis中
            List<Setmeal> setmealAll = setmealMapper.findSetmealAll();
            json = JSON.toJSONString(setmealAll);
            jedisPool.getResource().set("setmealAllRemote", json);
            System.out.println("从数据库中查询所有套餐");
        }else{
            System.out.println("从redis中查询所有套餐");
        }
        return json;
    }

    //根据id查询具体某个套餐
    @Override
    public String findById(String id) {
        //1.首先从redis中查询
        String json = jedisPool.getResource().hget("setmealSingleRemote", id);
        if (StringUtils.isEmpty(json)) {
            Setmeal setmeal = setmealMapper.findById(id);
            json = JSON.toJSONString(setmeal);
            jedisPool.getResource().hset("setmealSingleRemote",id,json);
            System.out.println("从数据库中查询具体套餐");
        }else{
            System.out.println("从redis中查询具体套餐");
        }
        return json;
    }
    /*
           第二个饼图
        */
    public Map getMemberReportBySex() {
        List<String> namesex=new ArrayList<String>();
        List<Map> lists=new ArrayList<Map>();
        for(Integer i=0;i<3;i++){
            String name;
            if(i==0){
                name="未知";
            }else if(i==1){
                name="男";
            }else{
                name="女";
            }
            namesex.add(name);
            Map<String,String> m=new HashMap<String,String>();
            Integer count= setmealMapper.findMemberBySex(i);
            m.put("name",name);
            m.put("value",count.toString());
            lists.add(m);
        }
        Map<String,Object> maps=new HashMap<String,Object>();
        maps.put("sexNames",namesex);
        maps.put("sexCounts",lists);
        return maps;
    }
}
